package com.zzyl.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.json.JSONUtil;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.LeaveDto;
import com.zzyl.dto.RetreatReqDto;
import com.zzyl.entity.Leave;
import com.zzyl.service.LeaveService;
import com.zzyl.service.UserService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.UserVo;
import com.zzyl.vo.retreat.TasVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/leave")
@Api(tags = "请假")
public class LeaveController {

    @Autowired
    private LeaveService leaveService;

    @PostMapping("/create")
    @ApiOperation(value = "申请请假", notes = "传入请假对象")
    public ResponseResult createLeave(
            @RequestBody @ApiParam(value = "请假对象", required = true) Leave leave) {
        return leaveService.createLeave(leave);
    }

    @GetMapping
    @ApiOperation(value = "请假表单查询")
    public ResponseResult<TasVo> getRetreat(
            @RequestParam @ApiParam(value = "请假编码") String leaveNo,
            @RequestParam(required = false) @ApiParam(value = "处理人ID") String assigneeId,
            @RequestParam @ApiParam(value = "流程状态") Integer flowStatus,
            @RequestParam(required = false) @ApiParam(value = "任务id")String taskId) {
        return leaveService.getLeave(leaveNo, assigneeId, flowStatus, taskId);
    }

    @PostMapping("/submit")
    @ApiOperation(value = "提交")
    public ResponseResult submitRetreat(
            @RequestBody @ApiParam(value = "请假对象", required = true) LeaveDto leaveDto) {
        return leaveService.submitRetreat(leaveDto);
    }

    @PutMapping
    @ApiOperation(value = "驳回")
    public ResponseResult disapprove(
            @RequestParam @ApiParam(value = "请假编码") String leaveNo,
            @RequestParam @ApiParam(value = "驳回消息") String message,
            @RequestParam @ApiParam(value = "任务Id") String taskId) {
        return leaveService.disapprove(leaveNo, message, taskId);
    }

    @PutMapping("/reject")
    @ApiOperation(value = "审核拒绝")
    public ResponseResult auditReject(
            @RequestParam @ApiParam(value = "请假编码") String leaveNo,
            @RequestParam @ApiParam(value = "拒绝原因") String reject,
            @RequestParam @ApiParam(value = "任务Id") String taskId) {
        return leaveService.auditReject(leaveNo, reject, taskId);
    }

    @PutMapping("/revocation")
    @ApiOperation(value = "撤回")
    public ResponseResult revocation(
            @RequestParam @ApiParam(value = "请假编码") String leaveNo,
            @RequestParam @ApiParam(value = "流程状态") Integer flowStatus,
            @RequestParam @ApiParam(value = "任务Id") String taskId) {
        return leaveService.revocation(leaveNo, flowStatus, taskId);
    }

    @PutMapping("/cancel")
    @ApiOperation(value = "撤销")
    public ResponseResult cancel(@RequestParam @ApiParam(value = "退住编码") String leaveNo,
                                 @RequestParam @ApiParam(value = "任务Id") String taskId){
        return leaveService.cancel(leaveNo, taskId);
    }


    @ApiOperation(value = "请假管理", notes = "请假管理列表页")
    @GetMapping("/selectByPage")
    public ResponseResult selectByPage(
            @ApiParam(value = "请假编码") @RequestParam(required = false) String leaveNo,
            @ApiParam(value = "姓名") @RequestParam(required = false) String name,
            @ApiParam(value = "身份证号") @RequestParam(required = false) String idCardNo,
            @ApiParam(value = "开始时间") @RequestParam(required = false) Long startTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) Long endTime,
            @ApiParam(value = "页码") @RequestParam(required = true,defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页数量") @RequestParam(required = true,defaultValue = "10") Integer pageSize
    ) {
        String subject = UserThreadLocal.getSubject();
        UserVo userVo = JSONUtil.toBean(subject, UserVo.class);
//        String deptNo = userVo.getDeptNo();
        Long userId = userVo.getId();

        LocalDateTime start = null;
        LocalDateTime end = null;
        if(startTime != null && endTime != null){
            start = LocalDateTimeUtil.of(startTime);
            end = LocalDateTimeUtil.of(endTime);
        }

        return leaveService.selectByPage(leaveNo, name, idCardNo, start, end, pageNum, pageSize, userId);
    }

}
